package controllers;

import java.lang.String;import java.util.List;

import controllers.CheckLogin;import models.TypeName;
import play.mvc.Controller;
import play.mvc.With;

@With(value = AdminCheck.class)
public class Types extends Controller {
	public static void index() {
		showType("goods");
	}

	// 显示类型
	public static void showType(String type) {
		List<String> nameList = TypeName.find(
				"select attrubite from TypeName group by attrubite").fetch();
		List<TypeName> typeList = TypeName.find(
				"attrubite = ? order by weight, createTime", type).fetch();
		if (nameList == null)
			index();
		String[][] typenameList = new String[nameList.size()][2];
		for (int i = 0; i < nameList.size(); i++) {
			typenameList[i][0] = nameList.get(i);
			typenameList[i][1] = getTypeChinese(nameList.get(i));
		}
		render(typenameList, typeList, type);
	}

	// 修改类型权重
	public static void editTypeWeight(int[] weight, String[] typeId) {
		if (weight == null || typeId == null || weight.length != typeId.length)
			index();
		int len = weight.length;
		TypeName tn = null;
		for (int i = 0; i < len; i++) {
			tn = TypeName.find("id = ?", typeId[i]).first();
			tn.weight = weight[i];
			tn.save();
		}
		showType(tn.attrubite);
	}

	// 删除类型
	public static void deleteType(String typeId) {
		TypeName tn = TypeName.find("id = ?", typeId).first();
		if (tn == null)
			index();
		tn.delete();
		showType(tn.attrubite);
	}

	// 添加类型
	public static void addType(TypeName typename) {
		if (typename == null || typename.attrubite == null
				|| "".equals(typename.attrubite))
			index();
		typename.save();
		showType(typename.attrubite);
	}

	// 类型名称的中文名称
	private static String getTypeChinese(String type) {
		if (type.equals("goods")) {
			return "商品类型";
		} else {
			return type;
		}
	}
}
